<?php
// +----------------------------------------------------------------------
// | [RhaPHP System] Copyright (c) 2017-2020 http://www.rhaphp.com/
// +----------------------------------------------------------------------
// | [RhaPHP] 并不是自由软件,你可免费使用,未经许可不能去掉RhaPHP相关版权
// +----------------------------------------------------------------------
// | Author: Geeson <qimengkeji@vip.qq.com>
// +----------------------------------------------------------------------
namespace addons\couponPin\controller;

use app\common\controller\Addon;
use think\Db;
use think\facade\Request;
/**
 * 拼多多查券机器人
 * qq:272984023
 * @author JYY
 */
class Coupon extends Addon
{

    public $adminLogin = true;

    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
    }

    public function lists()
    {
        $lists = Db::name('coupon_name')->where('mid', '=', $this->mid)->order('id DESC')->paginate(20);

        $this->assign('data', $lists);
        $this->fetch();
    }
	
	public function config(){
		if (Request::isAjax()) {
			$do = input('post.');
			$data['infos'] = json_encode($do);
			if (!is_numeric($do['owner'])) {
                ajaxMsg('0', '多多进宝账号错误！');
            }
			//$data['mid'] = $this->mid;
           
			if(Db::name('couponpin_config')->where('mpid', '=', $this->mid)->find()){
				if(Db::name('couponpin_config')->where('mpid', '=', $this->mid)->update($data)){
					ajaxMsg('1', '设置成功');
				} else {
                	ajaxMsg('0', '操作失败,设置数据没有变化。');
            	}
			}else{
				$data['mpid'] = $this->mid;
				if(Db::name('couponpin_config')->insert($data)){
					ajaxMsg('1', '设置成功');
				}else {
                	ajaxMsg('0', '操作失败。');
            	}
			}
		}else{
			$config = getConfig($this->mid);
			//print_r($config);
			$this->assign('data', $config);
        	$this->fetch();
		}
	}
	public function getpid(){
		//$info = getAddonInfo('couponPin');
		$info = getMpInfo();
		//print_r($info);die(1);
		$owner = input('owner');
		if(empty($owner)){
			$data['status'] = 1;
		}else{
			$url ="http://jyy.huiyuntk.cn/api/pddauth/getpid.html";
			$pdata = "d1={$this->mid}&d2={$info['name']}&d3={$info['mp_number']}&d4={$info['origin_id']}&d5={$info['type']}&d6=rha&d7=".$_SERVER['SERVER_NAME']."&d8=".$_SERVER['SERVER_NAME']."&akey=jyy2018&owner=".$owner;
			$pid = execcurl($url,true,$pdata);
			$pidarr = json_decode($pid,true);
			if($pidarr['status'] ==0){
				$pid1 =$pidarr['pid'];
				$data['status'] =0;
				$data['pid'] = $pid1;
			}else{
				$data['status'] = $pidarr['status'];
			}
		}
		$pid = json_encode($data);
		echo $pid;
	}
	
	public function url(){
		
	}
   
}